<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('key.xh')">
              <a-input v-model="queryParam.xh" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item :label="$t('key.name')">
              <a-input v-model="queryParam.xm" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.dsxm')">
                <a-input v-model="queryParam.dsxm" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.stuCate')">
                <a-select v-model="queryParam.xslbm" allowClear>
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.yx')">
                <a-select v-model="queryParam.ssyxm" allowClear @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.zy')">
                <a-select v-model="queryParam.sszym" allowClear showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zymc + '- ' + i.zyh }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item :label="$t('key.nj')">
                <a-select v-model="queryParam.sznj" allowClear>
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">{{ $t('button.query') }}</a-button>
              <a-button type="primary" @click="handleCheck">{{ $t('button.batchReview') }}</a-button>
              <a-button style="margin-left: 8px" @click="resetForm">{{ $t('button.reset') }}</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="xsxxId"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :scroll="{x: 1400}"
      :rowSelection="options.rowSelection">

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

      <a slot="operation" slot-scope="a, b" @click="checkTransfer(b)">{{ $t('button.audit') }}</a>
    </s-table>

    <check-modal
      ref="check"
      :visible="visible"
      :id="checkId"
      :data="checkData"
      @cancel="visible = false"
      @refresh="refresh"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import checkModal from './batchCheck'

export default {
  components: {
    STable,
    checkModal
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: this.$t('key.no'),
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.xh'),
          dataIndex: 'xh'
        },
        {
          title: this.$t('key.name'),
          dataIndex: 'xm'
        },
        {
          title: this.$t('key.enName'),
          dataIndex: 'ywxm'
        },
        {
          title: this.$t('key.yx'),
          dataIndex: 'ssyxmc'
        },
        {
          title: this.$t('key.zy'),
          dataIndex: 'sszymc'
        },
        {
          title: this.$t('key.nj'),
          dataIndex: 'sznj'
        },
        {
          title: this.$t('key.dsxm'),
          dataIndex: 'dsxm'
        },
        {
          title: this.$t('key.idCard'),
          dataIndex: 'zjhm'
        },
        {
          title: this.$t('key.stuCate'),
          dataIndex: 'xslbmc'
        },
        {
          title: this.$t('key.option'),
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: '100px',
          fixed:'right'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.student.uncheckedList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      visible: false,
      checkId: null,
      checkData: null,
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {}
    }
  },
  computed: {
    ...mapState({
      TYPE0070: state => state.app['TYPE0070'], // 异动类型
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0126: state => state.app['TYPE0126'], // 年级
      lang: state => state.account.lang
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0070')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    resetForm () {
      this.queryParam = {}
      this.selectMenu.zy = []
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    checkTransfer (item) {
      this.$router.push(`/stuStatus/check?xh=${item.xh}`)
      // this.$emit('toInfo', true)
    },
    refreshTable () {
      this.$refs.table.refresh(true)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    refresh () {
      this.selectedRowKeys = []
      this.$refs.table.refresh(true)
    },
    handleCheck() {
      if (!this.selectedRows.length) return this.$message.info('请选择要审核的内容！');
      this.visible = true
      this.checkId = this.selectedRowKeys.toString()
      this.checkData = this.selectedRows
    }
  }
}
</script>

<style>

</style>
